<?php
namespace adminmatrix\admin\annotation;

use ReflectionObject;
use ReflectionClass;
use adminmatrix\admin\annotation\Validate;
use adminmatrix\admin\service\JsonService;
use think\exception\ValidateException;
trait InteractsWithValidate
{
    protected function autoValidate()
    {
        $this->app->resolving(function($object, $app){
            if ($this->isInjectClass(get_class($object))) {
                $refClass = new ReflectionClass($object);
                
                // 只获取方法上的注解
                foreach ($refClass->getMethods() as $method) {
                    $attrs = $method->getAttributes(Validate::class);
                    if(!empty($attrs))
                    {
                        foreach($attrs as $attr) {
                            $instance = $attr->newInstance();
                            // var_dump([
                            //     'method' => $method->getName(),
                            //     'name' => $instance->name,
                            //     'method_type' => $instance->method,
                            //     'instance' => $instance
                            // ]);
                            if($instance->method != Request()->method())
                            {
                                return throw new \Exception("请求方式错误,请使用{$instance->method}请求");
                            }

                            $validate = $this->app->validate;
                            if($instance->ref)
                            {
                                $validate = $instance->ref;
                            }
                            
                            if(!empty($instance->rules))
                            {
                                $validate->rule($instance->rules);
                            }

                            if(!empty($instance->scene))
                            {
                                $validate->scene($instance->scene);
                            }

                            if(!empty($instance->message))
                            {
                                $validate->message($instance->message);
                            }

                            $check = false;
                            switch($instance->method)
                            {
                                case 'GET':
                                    $check = $validate->check(Request()->param());
                                    break;
                                case 'POST':
                                    $check = $validate->check(Request()->param());
                                    break;
                                case 'PUT':
                                    $check = $validate->check(Request()->param());
                                    break;
                                case 'DELETE':
                                    $check = $validate->check(Request()->param());
                                    break;
                                
                            }

                            if(!$check)
                            {
                                return throw new ValidateException($validate->getError());
                            }
                        }
                    }
                }
            }
        });
    }
}
